<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.blurry" clearable size="small" :placeholder="$t('Manage.searchPlaceholder6')" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <date-range-picker
          v-model="query.createTime"
          class="date-item"
          :start-placeholder="$t('datePicker.start')"
          :end-placeholder="$t('datePicker.end')"
        />
        <rrOperation />
      </div>
      <crudOperation :permission="permission" />
    </div>
    <!--表单渲染-->
    <el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="580px">
      <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="80px">
        <el-form-item :label="$t('menu.type')" prop="type">
          <el-radio-group v-model="form.type" size="mini" style="width: 178px">
            <el-radio-button label="0">{{ $t('menu.type0') }}</el-radio-button>
            <el-radio-button label="1">{{ $t('menu.type1') }}</el-radio-button>
            <el-radio-button label="2">{{ $t('menu.type2') }}</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-show="form.type.toString() !== '2'" :label="$t('menu.icon')" prop="icon">
          <el-popover
            placement="bottom-start"
            width="450"
            trigger="click"
            @show="$refs['iconSelect'].reset()"
          >
            <IconSelect ref="iconSelect" @selected="selected" />
            <el-input slot="reference" v-model="form.icon" style="width: 450px;" :placeholder="$t('Manage.searchPlaceholder7')" readonly>
              <svg-icon v-if="form.icon" slot="prefix" :icon-class="form.icon" class="el-input__icon" style="height: 32px;width: 16px;" />
              <i v-else slot="prefix" class="el-icon-search el-input__icon" />
            </el-input>
          </el-popover>
        </el-form-item>
        <el-form-item v-show="form.type.toString() !== '2'" :label="$t('menu.iFrame')" prop="iFrame">
          <el-radio-group v-model="form.iFrame" size="mini">
            <el-radio-button label="true">{{ $t('common.yes') }}</el-radio-button>
            <el-radio-button label="false">{{ $t('common.no') }}</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-show="form.type.toString() === '1'" :label="$t('menu.cache')" prop="cache">
          <el-radio-group v-model="form.cache" size="mini">
            <el-radio-button label="true">{{ $t('common.yes') }}</el-radio-button>
            <el-radio-button label="false">{{ $t('common.no') }}</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-show="form.type.toString() !== '2'" :label="$t('menu.hidden')" prop="hidden">
          <el-radio-group v-model="form.hidden" size="mini">
            <el-radio-button label="false">{{ $t('common.yes') }}</el-radio-button>
            <el-radio-button label="true">{{ $t('common.no') }}</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="form.type.toString() !== '2'" :label="$t('menu.title')" prop="title">
          <el-input v-model="form.title" :style=" form.type.toString() === '0' ? 'width: 450px' : 'width: 178px'" :placeholder="$t('menu.title')" />
        </el-form-item>
        <el-form-item v-if="form.type.toString() === '2'" :label="$t('menu.btnTitle')" prop="title">
          <el-input v-model="form.title" :placeholder="$t('menu.btnTitle')" style="width: 178px;" />
        </el-form-item>
        <el-form-item v-show="form.type.toString() !== '0'" :label="$t('menu.permission')" prop="permission">
          <el-input v-model="form.permission" :disabled="form.iFrame.toString() === 'true'" :placeholder="$t('menu.permission')" style="width: 178px;" />
        </el-form-item>
        <el-form-item v-if="form.type.toString() !== '2'" :label="$t('menu.path')" prop="path">
          <el-input v-model="form.path" :placeholder="$t('menu.path')" style="width: 178px;" />
        </el-form-item>
        <el-form-item :label="$t('menu.menuSort')" prop="menuSort">
          <el-input-number v-model.number="form.menuSort" :min="0" :max="999" controls-position="right" style="width: 178px;" />
        </el-form-item>
        <el-form-item v-show="form.iFrame.toString() !== 'true' && form.type.toString() === '1'" :label="$t('menu.componentName')" prop="componentName">
          <el-input v-model="form.componentName" style="width: 178px;" :placeholder="$t('Manage.searchPlaceholder8')" />
        </el-form-item>
        <el-form-item v-show="form.iFrame.toString() !== 'true' && form.type.toString() === '1'" :label="$t('menu.component')" prop="component">
          <el-input v-model="form.component" style="width: 178px;" :label="$t('menu.component')" />
        </el-form-item>
        <el-form-item :label="$t('menu.pid')" prop="pid">
          <treeselect
            v-model="form.pid"
            :options="menus"
            :load-options="loadMenus"
            style="width: 450px;"
            :placeholder="$t('Manage.searchPlaceholder9')"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">{{ $t('crud.cancel') }}</el-button>
        <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">{{ $t('crud.confirm') }}</el-button>
      </div>
    </el-dialog>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      lazy
      :load="getMenus"
      :data="crud.data"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      row-key="id"
      @select="crud.selectChange"
      @select-all="crud.selectAllChange"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column :show-overflow-tooltip="true" :label="$t('menu.title')" width="125px" prop="title" />
      <el-table-column prop="icon" :label="$t('menu.icon2')" align="center" width="60px">
        <template slot-scope="scope">
          <svg-icon :icon-class="scope.row.icon ? scope.row.icon : ''" />
        </template>
      </el-table-column>
      <el-table-column prop="menuSort" align="center" :label="$t('menu.menuSort2')">
        <template slot-scope="scope">
          {{ scope.row.menuSort }}
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="permission" :label="$t('menu.permission')" />
      <el-table-column :show-overflow-tooltip="true" prop="component" :label="$t('menu.component')" />
      <el-table-column prop="iFrame" :label="$t('menu.iFrame2')" width="75px">
        <template slot-scope="scope">
          <span v-if="scope.row.iFrame">{{ $t('common.yes') }}</span>
          <span v-else>{{ $t('common.no') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="cache" :label="$t('menu.cache2')" width="75px">
        <template slot-scope="scope">
          <span v-if="scope.row.cache">{{ $t('common.yes') }}</span>
          <span v-else>{{ $t('common.no') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="hidden" :label="$t('menu.hidden2')" width="75px">
        <template slot-scope="scope">
          <span v-if="scope.row.hidden">{{ $t('common.yes') }}</span>
          <span v-else>{{ $t('common.no') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" :label="$t('menu.createTime')" width="135px" />
      <el-table-column v-if="checkPer(['admin','menu:edit','menu:del'])" :label="$t('common.operation')" width="130px" align="center" fixed="right">
        <template slot-scope="scope">
          <udOperation
            :data="scope.row"
            :permission="permission"
            :msg="$t('Manage.searchPlaceholder10')"
          />
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import crudMenu from '@/api/system/menu'
import IconSelect from '@/components/IconSelect'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import DateRangePicker from '@/components/DateRangePicker'
import i18n from '@/lang'
// crud交由presenter持有
const defaultForm = { id: null, title: null, menuSort: 999, path: null, component: null, componentName: null, iFrame: false, roles: [], pid: 0, icon: null, cache: false, hidden: false, type: 0, permission: null }
export default {
  name: 'Menu',
  components: { Treeselect, IconSelect, crudOperation, rrOperation, udOperation, DateRangePicker },
  cruds() {
    return CRUD({ title: '菜单', url: 'api/menus', crudMethod: { ...crudMenu }})
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  data() {
    return {
      menus: [],
      permission: {
        add: ['admin', 'menu:add'],
        edit: ['admin', 'menu:edit'],
        del: ['admin', 'menu:del']
      },
      rules: {
        title: [
          { required: true, message: i18n.t('rules.inputTitle'), trigger: 'blur' }
        ],
        path: [
          { required: true, message: i18n.t('rules.inputAddress'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      this.menus = []
      if (form.id != null) {
        if (form.pid === null) {
          form.pid = 0
        }
        this.getSupDepts(form.id)
      } else {
        this.menus.push({ id: 0, label: '顶级类目', children: null })
      }
    },
    getMenus(tree, treeNode, resolve) {
      const params = { pid: tree.id }
      setTimeout(() => {
        crudMenu.getMenus(params).then(res => {
          resolve(res.content)
        })
      }, 100)
    },
    getSupDepts(id) {
      crudMenu.getMenuSuperior(id).then(res => {
        const children = res.map(function(obj) {
          if (!obj.leaf && !obj.children) {
            obj.children = null
          }
          return obj
        })
        this.menus = [{ id: 0, label: '顶级类目', children: children }]
      })
    },
    loadMenus({ action, parentNode, callback }) {
      if (action === LOAD_CHILDREN_OPTIONS) {
        crudMenu.getMenusTree(parentNode.id).then(res => {
          parentNode.children = res.map(function(obj) {
            if (!obj.leaf) {
              obj.children = null
            }
            return obj
          })
          setTimeout(() => {
            callback()
          }, 100)
        })
      }
    },
    // 选中图标
    selected(name) {
      this.form.icon = name
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
 ::v-deep .el-input-number .el-input__inner {
    text-align: left;
  }
 ::v-deep .vue-treeselect__control,::v-deep .vue-treeselect__placeholder,::v-deep .vue-treeselect__single-value {
    height: 30px;
    line-height: 30px;
  }
</style>
